<{capture name="header"}>
<{css app="ome" src="ome.css"}>
<{css app="ome" src="style.css"}>
<{script src="coms/modedialog.js" app="desktop"}>
<{script src="coms/pager.js" app="desktop"}>
<{/capture}>
<form method="post" id="addPkgForm" action="index.php?app=ome&ctl=admin_preprocess_tbgift&act=save">
  <div class="tableform">
    <div class="division">
      <h5>赠品基本信息</h5>
      <table>
      <tr><td>赠品名称：<input type="text" vtype="required" name="name"></td></tr>
      <tr><td>赠品编码：<input type="text" vtype="required" name="gift_bn"></td>
      <input type="hidden"  id="goods_type" name="goods_type" value="normal">
      </tr>
      <tr><td>状态：<input name="status" type="radio" value=1 >启用<input name="status" type="radio" value=2>关闭<{help}>关闭后赠品无效，不可在业务处理流程中使用<{/help}></td></tr>
      </table>
      <h5 style="margin-top:10px;">赠品对应的销售物料信息</h5>
      <{button label="添加销售物料" id="purchase-find-btn"}>
      <table class="gridlist" id="purchase_table"  style="margin:4px 0;">
        <thead>
          <tr>
            <th>基础物料编码</th>
            <th>基础物料名称</th>
            <th style="width:30px;">删除</th>
          </tr>
        </thead>
        <tbody id="dataNode">
          <tr>
            <td colspan="7" style="padding:0;"><div class="note" style="margin:0;"> 暂无对应的销售物料信息</div></td>
          </tr>
        </tbody>
      </table>
      <div align="right">
        <{button type="button" id="purchase-delall-btn" label="全部删除" }></div> </div>
    <div id="cc" class="noprint table-action"> <{button type="button" class="btn" id="purchase-save-btn" label="保 存"}> &nbsp; <{button type="button" class="btn" id="return-btn" label="返 回" onclick="javascript:window.close();"}></div>
  </div>
</form>
<script>
(function(){
var visiTips = new Tips({
		onShow:function(tip,el){
			el.addClass('active');
			tip.setStyle('display','block');
		},
		text: function(element){
			if(element.get('visibility')=='false'){
				return '不可售商品';
			}else{
				return '';
			}
		}
});

    var callurl='index.php?app=material&ctl=admin_material_sales&act=getSalesMaterial',store=[];

    var tpl='<tr key="{sm_id}" id="sm_{sm_id}" title="点击选取/反选此条记录">'
        +'  <td>{sales_material_bn}<input type="hidden" name="pid[]" value="{sm_id}"></td><td class="material-name" >{sales_material_name}</td>'
        +'  <td><{img src="bundle/delecate.gif" app="desktop" key="state" class="pointer btn-delete-item"}></td>'
        +'</tr>';

    $('purchase-find-btn').addEvent('click',function(e){
        var url='index.php?app=desktop&act=alertpages&goto='+encodeURIComponent('index.php?app=material&ctl=admin_material_sales&act=findSalesMaterial&type=3');
        new finderDialog(url,{params:{url:callurl,name:'sm_id[]'},width:1000,height:660,
            onCallback:function(rs){
                if(!rs)return;
                rs=JSON.decode(rs);
                init(rs);
            }
        });
    });

    var pag,rows;
    function emptyData(){
        var noData='<tr>'
            +'<td colspan="7" style="padding:0;"><div class="note" style="margin:0;"> 暂无对应的货品信息 </div></td>'
            +'</tr>';
        $('dataNode').set('html',noData);
    }

    function createProduct(data){
        pag=new PageData(tpl,data,{'updateMain':$('dataNode'),'pageNum':15,PRIMARY_ID:'sm_id',
        'onShow':function(){
             var _this=this;
             $$('#dataNode input[type]').addEvent('change',function(e){
                 var pid=this.getParent('tr').get('key'),value=this.value;
                _this.editData(pid,[this.get('key'),value]);
            });

            rows=$ES('#dataNode tr');
            rows.each(function(item,i){
                item.addEvent('click',function(e){
                    this.toggleClass('selected');
                });
                item.getElement('.btn-delete-item').addEvent('click',function(e){
                    if(_this.selectData(item.get('key')) && confirm('确定要删除 '+ _this.selectData(item.get('key'))['name'] +' 吗？')) _this.delData(item.get('key'));
                    if(!$E('#dataNode tr')) emptyData();
                });
            });
            
			$ES('.product-name').each(function(item){
				if(item.get('visibility')=='false'){
					item.setStyle('color','#808080');
				}
			});
			$ES('.product-name').removeEvent('mouseover').addEvent('mouseover',function(e){
				if (this.get('visibility')=='false')
				{
					var e  = new Event(e), el = e.target;
					visiTips.attach(el);
					el.addEvent('mouseleave',function(){
						this.removeClass('active');
					});
					el.fireEvent('mouseenter',e);
				}
		  });
        }
        });
    }
    function init(rs){
        var tmparr=findProduct(rs,'sm_id');
         store.unshift.apply(store,tmparr.reverse());
         createProduct(store);
    }
    function findProduct(arr,PRIMARY){
        if(!store.length)return arr;
        store.each(function(a){
            arr.each(function(b){
                if(a[PRIMARY]==b[PRIMARY])arr.erase(b);
            });
        });
        return arr;
    }

    function delProduct(obj,arr){
        arr.each(function(d){obj.delData(d);});
    }

    $('purchase-delall-btn').addEvent('click',function(e){
        if(!pag||!pag.data)return;
        var delarr=[];
        pag.data.each(function(d){
             delarr.push(d['sm_id']);
        });
        if(confirm('确认删除全部货品吗？')){
            delProduct(pag,delarr);
            emptyData();
        }
    });


    $('purchase-save-btn').addEvent('click',function(e){
        var form=this.getParent('form');
        if(pag){
          var data=pag.toHideInput($('dataNode').getElement('tr'));
             form.store('target',{extraData:data,
              onComplete:function(jsontext){
                  try{
                      var json = Json.evaluate(jsontext);
                      if (typeof(json.error) == 'undefined'){
                          opener.finderGroup['<{$env.get.finder_id}>'].refresh.delay(100,opener.finderGroup['<{$env.get.finder_id}>']);
                          setTimeout('window.close()',200);
                      }
                  }catch(e){

				  }
              }});
        }
        form.fireEvent('submit',e);
    });
})();

function chgexigence(o){
    o.checked?$("exigence").show():$("exigence").hide();
}
function validSupplier(element){
    element=$(element);
    new Element('span',{'class':'error caution notice-inline','html':'本项必填'}).injectAfter(element);
    element.removeEvents('blur').addEvent('blur',function(){
       if(validate(element)) element.removeEvent('blur',arguments.callee);
    });
}


function pushDate(o){
    if(o.value!==""){
        if(/^\d*$/.test(o.value)){
            if (o.getNext('.error')) o.getNext('.error').dispose();
            $("pushdate").set("text",(new Date(Date.parse(new Date())+86400000 * o.value.toInt())).toLocaleDateString());
        }
        else o.value="";
    }
    else $("pushdate").empty();
}

function prichange(ss){
    if (ss == 1){
        $$('.endd').hide();
        $('price').set('value', '');
    }else{
        $$('.endd').show();
    }
}

function checkName(thisval){
    if(/!|！/.test(thisval)){
        var val = thisval.replace("!","");
        val = val.replace("！","");
        $('purchase_name').set('value', val);
    }
//切换赠品类型时，清除
function changeType(){
    if(!pag||!pag.data)return;
    var delarr=[];
    pag.data.each(function(d){
         delarr.push(d['sm_id']);
    });
    if(confirm('确认删除全部货品吗？')){
        delProduct(pag,delarr);
        emptyData();
    }
}
}
</script>
